/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_SBF_
#define	_INC_AK_SBF_

#include "ak_types.h"
#include "ak_lru.h"
#include "ak_task.h"

#ifdef __cplusplus
extern "C" {
#endif

/* ------------------------------------------------------------------
**	Sys Back Fill Cb Pool
*/
typedef struct _t_SBF_CbPool{
	_t_sysCS 			lock;
	
	_t_blist 			queue;
	int 				type;
	int 				incrMax;
	int 				incrCnt;
	unsigned int 		incrSz;
}_t_SBF_CbPool;


struct 	_t_SBF_CcCtx;
/* ------------------------------------------------------------------
**	SM Back Fill Cb Tree
*/
typedef struct _t_SBF_CbTree{
	_t_sysLruCt 			ct; 

#if 0
	_t_rbroot 				kTree;	/* The RB-Tree of all Key-Value Pairs */
	_t_rbroot 				lTree;	/* The RB-Tree of all Lazy LRU node */

	unsigned int 			index;
	/* GC */
	time_t 					lazy;	/* Trigger the GC, May Lazy than HashTbl's Current Tick */
#endif

	struct 	_t_SBF_CcCtx *	pCcCtx; 
	
	_t_SBF_CbPool 		*	pSync;
	_t_SBF_CbPool 		*	pASync;
}_t_SBF_CbTree;


#define SBF_TREE_MAX 		5
/* ------------------------------------------------------------------
**	SM Back Fill Cb Tree
*/
typedef struct _t_SBF_CcCtx{
	_t_bnode 			node;

	unsigned long 		casId;

	_t_sysKLFunc 		klFunc;
	_t_SBF_CbTree 		cbTree[SBF_TREE_MAX];
	
	/* CB Pool */
	_t_SBF_CbPool 		poolSync;
	_t_SBF_CbPool 		poolASync;

	/* GC */
	time_t 				current;
	
	_t_sysSemB 			gcWake;
	_t_sysTask 			thread;
	_t_sysTaskAttr 		tAttr;
	
}_t_SBF_CcCtx;


/* =====================================================================
**	 Session Async Call-Back TimeOut Tree Definition:
*/
#define SBF_CB_ASYNC		0X00000010	
#define SBF_CB_SYNC			0X00000020	
#define SBF_CB_A_MASK 		0X000000F0

typedef struct _t_SBF_CbNode{
	_t_sysLruCi 			ci;
	#if 0
	union{
		_t_rbnode			kNode;		/* Key-Value RB-Tree Node */
		_t_bnode			aNode;		/* Allocator Node */
	};
	
	_t_rbnode				lNode;		/* Lazy LRU RB-Tree Node */
	_t_snode 				sNode;

	unsigned long 			casId;		/* the CAS identifier */
	time_t 					expire;		/* expire time */
	unsigned int 			type;
	#endif
	
	union{
		/* For Sync Interface */
		struct{	
			_t_sysSemB 		*	pWake;			
			void 			* 	outBuf;
			int				* 	outLen;
		};
		/* For ASync Interface */
		_t_cbentry 				cbEntry;
	};
}_t_SBF_CbNode;

/* ------------------------------------------------------------------
**	SM Back Fill Interface
*/
extern	int 	SBF_Init(	_t_SBF_CcCtx  * pCcCtx);
extern	_t_SBF_CbNode *	SBF_Apply(_t_SBF_CcCtx * pCcCtx, _t_cbentry *	pCbEntry, int timeout);
extern	_t_SBF_CbNode *	SBF_ApplyEx(_t_SBF_CcCtx * pCcCtx, unsigned char * outBuf, int * outLen, int timeout);

extern	int 	SBF_CcCb(_t_SBF_CcCtx * pCcCtx, long lArg, void * pMsg, int lMsg, unsigned long casId);
extern	int 	SBF_CcTrigger(_t_SBF_CcCtx * pCcCtx);

#ifdef __cplusplus
}
#endif

#endif


